package com.checkoutcheckins.client.clickhandlers;

import com.checkoutcheckins.client.rpc.FoursquareRetrival;
import com.checkoutcheckins.client.services.FoursquareServiceAsync;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ShoutButtonHandler implements ClickHandler {

	private CheckBox facebookCheckbox;
	private CheckBox twitterCheckbox;
	private TextArea shoutMessage;
	private String foursquareId;
	private PopupPanel popup;

	public ShoutButtonHandler(PopupPanel popup, String foursquareId,
			CheckBox facebookCheckbox, CheckBox twitterCheckbox,
			TextArea shoutMessage) {
		this.facebookCheckbox = facebookCheckbox;
		this.twitterCheckbox = twitterCheckbox;
		this.shoutMessage = shoutMessage;
		this.foursquareId = foursquareId;
		this.popup = popup;
	}

	@Override
	public void onClick(ClickEvent event) {

		VerticalPanel loadingPanel = new VerticalPanel();
		loadingPanel.add(new HTML("<center>Message sent!  Word spread!  Thanks!</center>"));

		popup.clear();
		popup.add(loadingPanel);

		FoursquareServiceAsync fsSvc = FoursquareRetrival
				.getFoursquareService();

		boolean twitterVal = true;
		boolean facebookVal = true;

		if (twitterCheckbox == null || twitterCheckbox.getValue() == false) {
			twitterVal = false;
		}
		if (facebookCheckbox == null || facebookCheckbox.getValue() == false) {
			facebookVal = false;
		}

		fsSvc.sendShoutMessageToFoursquare(foursquareId,
				shoutMessage.getText(), twitterVal, facebookVal,
				new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						popup.hide();
					}

					@Override
					public void onSuccess(Boolean result) {
						popup.hide();
					}
				});
	}

}
